package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.orewar.UserOreData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserOreDataDaoImpl extends TempDataObjectDao<UserOreData> {

    public Map<Long, UserOreData> getUserOreDataMap() {
        Map<Long, UserOreData> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_activity_orewar_detail";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UserOreData data = parse(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public UserOreData getUserOreData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_activity_orewar_detail where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserOreData data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(UserOreData data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_activity_orewar_detail(userId,totalRobTimes,totalBeRobTimes,maxTeamReward1,maxTeamReward2,maxTeamReward3,totalReward1,totalReward2,"
                + "totalReward3) VALUES (?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getTotalRobTimes());
            call.setLong(i++, data.getTotalBeRobTimes());
            call.setString(i++, data.getMaxTeamReward1().toString());
            call.setString(i++, data.getMaxTeamReward2().toString());
            call.setString(i++, data.getMaxTeamReward3().toString());
            call.setString(i++, data.getTotalReward1().toString());
            call.setString(i++, data.getTotalReward2().toString());
            call.setString(i++, data.getTotalReward3().toString());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserOreData data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_activity_orewar_detail SET totalRobTimes=?,totalBeRobTimes=?,maxTeamReward1=?,maxTeamReward2=?,maxTeamReward3=?,totalReward1=?,totalReward2=?,"
                + "totalReward3=? where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getTotalRobTimes());
            call.setLong(i++, data.getTotalBeRobTimes());
            call.setString(i++, data.getMaxTeamReward1().toString());
            call.setString(i++, data.getMaxTeamReward2().toString());
            call.setString(i++, data.getMaxTeamReward3().toString());
            call.setString(i++, data.getTotalReward1().toString());
            call.setString(i++, data.getTotalReward2().toString());
            call.setString(i++, data.getTotalReward3().toString());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserOreData parseData(ResultSet rs) throws SQLException {
        UserOreData data = new UserOreData();
        data.setUserId(rs.getLong("userId"));
        data.setTotalRobTimes(rs.getLong("totalRobTimes"));
        data.setTotalBeRobTimes(rs.getLong("totalBeRobTimes"));
        String value = rs.getString("maxTeamReward1");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setMaxTeamReward1(new BigInteger(value));
        }
        value = rs.getString("maxTeamReward2");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setMaxTeamReward2(new BigInteger(value));
        }
        value = rs.getString("maxTeamReward3");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setMaxTeamReward3(new BigInteger(value));
        }
        value = rs.getString("totalReward1");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setTotalReward1(new BigInteger(value));
        }
        value = rs.getString("totalReward2");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setTotalReward2(new BigInteger(value));
        }
        value = rs.getString("totalReward3");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setTotalReward3(new BigInteger(value));
        }
        return data;
    }
}
